/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tsystems.school.simpleclient;

import com.google.gson.Gson;
import com.tsystems.school.server.*;
import com.tsystems.school.nutils.Message;
import com.tsystems.school.nutils.MessageType;
import java.awt.Color;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 123
 */
public class ClientWorker implements Runnable {

    protected Socket serverSocket;
    private String serverInfo;
    private BufferedReader input;
    private PrintWriter output;
    private Frame mf;
    private boolean login = false;

    public ClientWorker(Socket serverSocket, Frame mf) {
        this.serverSocket = serverSocket;
        this.mf = mf;
        serverInfo = "server: " + serverSocket.getInetAddress() + ": " + serverSocket.getPort();
        System.out.println("new " + serverInfo + "\n");


    }

    public ClientWorker(Socket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public void sendMessage(Message msg) {
        //  output.println(gs.toJson(msg));
        output.println(msg.toJSON());
    }

    @Override
    public void run() {
        try {
            input = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
            output = new PrintWriter(serverSocket.getOutputStream(), true);

            String str;


            while ((str = input.readLine()) != null) {
                System.out.println(str);
                if (login == true) {
                    Date date = new Date();
                    mf.addMessage(gs.fromJson(str, Message.class).getReceiver(), gs.fromJson(str, Message.class).getBody() + "\n", Color.red, date);
                    mf.writeHistory(gs.fromJson(str, Message.class).getReceiver(), gs.fromJson(str, Message.class).getBody(), date);
                } else {
                    
                    Message ans = Message.LoadFromJSON(str);
                    MessageType mtype = ans.getType();
                    System.out.println(mtype);
                    if (mtype.equals(MessageType.CANCEL)) {
                        JOptionPane.showMessageDialog(null, ans.getPropertyValue("result"));
                    }
                    if (mtype.equals(MessageType.OK)) {
                        login = true;
                        mf.dispose();
                        mf = new MainForm(this);
                        mf.setVisible(true);
                    } else {
                        JOptionPane.showMessageDialog(null, "could not recognise message");
                    }
                }
            }
            System.out.println("THE END");
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
